package org.example.maven.controller;

import org.example.maven.annotation.ReadOnlyMode;
import org.example.maven.annotation.logAnnotation;
import org.example.maven.pojo.Result;
import org.example.maven.pojo.position.AddPosition;
import org.example.maven.pojo.position.SearchPosition;
import org.example.maven.pojo.utils.Ids;
import org.example.maven.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/position")
public class PositionController {
    @Autowired
    private PositionService positionService;
    @GetMapping
    @PreAuthorize("permitAll()")
    //获取岗位的listTable信息
    public Result getAllPositionListTable(@RequestParam("pageNum") Integer pageNum,@RequestParam("pageSize") Integer pageSize){
        Result res=new Result();
        res.put("data",positionService.getAllPositionList(pageNum,pageSize));
        res.put("total",positionService.getPositionCount(new SearchPosition()));
        return res;
    }

    @PostMapping
    @ReadOnlyMode
    @PreAuthorize("hasAnyAuthority('position:show')")
    @logAnnotation(
            type = "新增",
            detail = "新增岗位{#addPosition.positionName}"
    )
    //添加岗位
    public Result addPosition(@RequestBody AddPosition addPosition){
        AddPosition addPosition1=positionService.getPositionLive(addPosition.getPositionName());
        AddPosition addPosition2=positionService.getPositionIsDeletedLive(addPosition.getPositionName());
        System.out.println(addPosition1);
        System.out.println(addPosition2);
        if(addPosition1==null){
            if(addPosition2==null){
                positionService.addPosition(addPosition);
                return Result.success("新增成功");
            }
            else {
                addPosition2.setIsDeleted(0);
                addPosition2.setPositionName(addPosition.getPositionName());
                addPosition2.setDeptId(addPosition.getDeptId());
                addPosition2.setStatus(addPosition.getStatus());
                addPosition2.setResponsibility(addPosition.getResponsibility());
                addPosition2.setMaxHeadcount(addPosition.getMaxHeadcount());
                addPosition2.setTime(addPosition.getTime());
                addPosition2.setCurrentHeadcount(0);
                positionService.updatePositionIsDeleted(addPosition2);
                return Result.success("新增成功");
            }
        }else {
            return Result.error("岗位已存在，新增失败");
        }
    }


    @GetMapping("/getPositionCount")
    @PreAuthorize("permitAll()")
    //获取岗位的total
    public Result getPositionCount(SearchPosition searchPosition){
        return Result.success(positionService.getPositionCount(searchPosition));
    }

    @GetMapping("/getSpeciePosition")
    @PreAuthorize("hasAnyAuthority('position:show')")
    //获取特定的岗位信息
    public Result getSpeciePosition(SearchPosition searchPosition,@RequestParam("pageNum") Integer pageNum,@RequestParam("pageSize") Integer pageSize){
        Result ajax = Result.success();
        System.out.println(searchPosition);
        ajax.put("data",positionService.getSpeciePosition(searchPosition,pageNum,pageSize));
        ajax.put("total",positionService.getPositionCount(searchPosition));
        return Result.success(ajax);
    }

    @PatchMapping
    @ReadOnlyMode
    @PreAuthorize("hasAnyAuthority('position:show')")
    @logAnnotation(
            type = "删除",
            detail = "删除岗位"
    )
    //删除岗位
    public Result deletePosition(@RequestBody Ids ids){
        positionService.deletePosition(ids.getId());
        return Result.success("删除成功");
    }

    @GetMapping("/getEditPositionDetail")
    @PreAuthorize("hasAnyAuthority('position:show')")
    //获取待编辑的岗位信息
    public Result getPositionEditDetail(@RequestParam("id") Integer id){
        return Result.success(positionService.getEditPosition(id));
    }

    @PutMapping
    @ReadOnlyMode
    @PreAuthorize("hasAnyAuthority('position:show')")
    @logAnnotation(
            type = "更新",
            detail = "更新岗位{#addPosition.positionName}"
    )
    //更新岗位信息
    public Result updatePosition(@RequestBody AddPosition addPosition){
        positionService.updatePosition(addPosition);
        return Result.success("修改成功");
    }

    @GetMapping("/getPositionByDepartmentId")
    @PreAuthorize("permitAll()")
    //根据部门id获取岗位信息
    public Result getPositionByDepartmentId(@RequestParam(name = "id",required = false) Integer id){
        return Result.success(positionService.getPositionByDepartmentId(id));
    }
}
